﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace Listas
{
    public class Memory
    {
        private ArrayList memoriaLivre = new ArrayList();
        private static int tamMemo = 12;
        Hashtable controleMemoria = new Hashtable();
        ArrayList posicoesVaziasMemoria = new ArrayList();
        int posMemoLivre = 0;

        int posMemoria = 0;


        public Memory()
        {

        }

        public void createMemory(Grid Gridi)
        {
            for (int i = 0; i < tamMemo; i++)
            {
                Label tempLabel = new Label();
                Gridi.Children.Add(tempLabel);
                memoriaLivre.Add(tempLabel);
            }

            foreach (Label lbl in memoriaLivre)
            {

                lbl.Visibility = Visibility.Visible;
                lbl.VerticalAlignment = VerticalAlignment.Bottom;
                lbl.HorizontalAlignment = HorizontalAlignment.Stretch;
                lbl.Foreground = Brushes.Firebrick;
                lbl.BorderBrush = Brushes.White;
                lbl.Background = Brushes.Transparent;
                lbl.BorderThickness = new Thickness(2);
                lbl.HorizontalContentAlignment = HorizontalAlignment.Center;
                lbl.VerticalContentAlignment = VerticalAlignment.Center;

                lbl.FontSize = 30;
                lbl.FontWeight = FontWeights.Bold;

                Grid.SetRow(lbl, 0);
                Grid.SetRowSpan(lbl, 3);
                Grid.SetColumn(lbl, 0);
                Grid.SetColumnSpan(lbl, 9);
            }
            
            ((Label)memoriaLivre[0]).Margin = new Thickness(800, 50, 140, 275);
            ((Label)memoriaLivre[1]).Margin = new Thickness(855, 50, 85, 275);
            ((Label)memoriaLivre[2]).Margin = new Thickness(910, 50, 35, 275);
            ((Label)memoriaLivre[3]).Margin = new Thickness(800, 75, 140, 250);
            ((Label)memoriaLivre[4]).Margin = new Thickness(855, 75, 85, 250);
            ((Label)memoriaLivre[5]).Margin = new Thickness(910, 75, 35, 250);
            ((Label)memoriaLivre[6]).Margin = new Thickness(800, 100, 140, 225);
            ((Label)memoriaLivre[7]).Margin = new Thickness(851, 100, 85, 225);
            ((Label)memoriaLivre[8]).Margin = new Thickness(910, 100, 35, 225);
            ((Label)memoriaLivre[9]).Margin = new Thickness(800, 125, 140, 200);
            ((Label)memoriaLivre[10]).Margin = new Thickness(855, 125, 85, 200);
            ((Label)memoriaLivre[11]).Margin = new Thickness(910, 125, 35, 200);
            

        }

        private void inicializaMemoriaLivre()
        {
            for (int i = 0; i < 12; i++)
            {
                posicoesVaziasMemoria.Add(i);
            }
        }

        public void setAllHidden()
        {
            for (int i = 0; i < tamMemo; i++)
            {
                ((Label)memoriaLivre[i]).Background = Brushes.Transparent;
            }
        }

        internal void initFreeMemory()
        {
            posicoesVaziasMemoria.Clear();
            inicializaMemoriaLivre();
            controleMemoria.Clear();
        }

        internal int posMemo
        {
            get { return this.posMemoria; }
        }

        internal int freePos
        {
            get { return this.posMemoLivre; }
        }

        internal void updateFreeMemory(int contElem)
        {
            controleMemoria.Add(contElem.ToString(), (int)posicoesVaziasMemoria[0]);

            posMemoLivre = (int)posicoesVaziasMemoria[0];

            ((Label)memoriaLivre[(int)posicoesVaziasMemoria[0]]).Background = Brushes.Thistle;

            posicoesVaziasMemoria.RemoveAt(0);
        }

        internal void updateFreeMemory(string p)
        {
            foreach (DictionaryEntry pm in controleMemoria)
            {
                if (pm.Key.ToString() == p)
                {
                    posMemoria = (int)pm.Value;
                    break;
                }
            }

            posicoesVaziasMemoria.Add(posMemoria);
            controleMemoria.Remove(p);
        }

        internal Label getMemory(int ind)
        {
            return (Label)this.memoriaLivre[ind];
        }
    }
}
